package com.gameapplet.util;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import org.xml.sax.InputSource;

import java.io.*;
import java.net.URL;
import java.util.Hashtable;
import java.util.List;

public class JDOMXMLBaseObject implements Serializable {

    public String[] _ResString1_ = {"<", ">", "&", "\"", "'"};
    public String[] _ResString2_ = {"&lt;", "&gt;", "&amp;", "&quot;", "&apos;"};
    public String XMLString;
    public SAXBuilder SAXParser = null;
    public Document Doc = null;
    public Element Root = null;
    public String ROOT_NAME = "XML_ROOT";

    public static void getAttributes(Element e, Hashtable list) {
        if ((e == null) || (list == null)) return;
        List alist = e.getAttributes();
        Attribute a = null;
        for (int i = 0; i < alist.size(); ++i) {
            a = (Attribute) alist.get(i);
            list.put(a.getName(), a.getValue());
        }
    }


    public JDOMXMLBaseObject() {
        CreateXmlEmpty();
    }

    public void CreateXmlEmpty() {
        try {
            this.SAXParser = new SAXBuilder();
            this.Root = new Element(this.ROOT_NAME);
            this.Doc = new Document();
            this.Doc.setRootElement(this.Root);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JDOMXMLBaseObject(String DataString) {
        try {
            this.SAXParser = new SAXBuilder();

            if (DataString.indexOf("<?xml") != -1)
                InitXMLString(DataString);
            else
                InitXMLFile(DataString);
        } catch (Exception e) {
            CreateXmlEmpty();
            e.printStackTrace();
        }
    }

    public boolean InitXMLString(String DataString) {
        boolean Res = false;
        try {
            this.SAXParser = new SAXBuilder();

            StringReader sr = new StringReader(DataString);
            InputSource iSrc = new InputSource(sr);
            this.Doc = this.SAXParser.build(iSrc);
            this.Root = this.Doc.getRootElement();
            Res = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res;
    }

    public boolean InitXMLFile(String DataString) {
        boolean Res = false;
        try {
            File XmlFile = new File(DataString);
            this.Doc = this.SAXParser.build(XmlFile);
            this.Root = this.Doc.getRootElement();
            Res = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res;
    }

    public JDOMXMLBaseObject(File XmlFile) {
        try {
            this.Doc = this.SAXParser.build(XmlFile);
            this.Root = this.Doc.getRootElement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JDOMXMLBaseObject(URL url) {
        try {
            this.SAXParser = new SAXBuilder();
            this.Doc = this.SAXParser.build(url);
            this.Root = this.Doc.getRootElement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean InitXMLURI(String uri) {
        boolean Res = false;
        this.SAXParser = new SAXBuilder();
        try {
            URL url = new URL(uri);
            this.Doc = this.SAXParser.build(url);
            this.Root = this.Doc.getRootElement();
            Res = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res;
    }

    public boolean InitXMLStream(InputStream is) {
        boolean Res = false;
        this.SAXParser = new SAXBuilder();
        try {
            this.Doc = this.SAXParser.build(is);
            this.Root = this.Doc.getRootElement();
            Res = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res;
    }

    public Element CreateElement(String ElementName) {
        Element node = null;
        node = new Element(ElementName);
        return node;
    }

    public List BeginEnumerate(Element ParentNode) {
        Element Parent = null;
        Parent = ParentNode;
        if (Parent == null) Parent = this.Root;
        List list = Parent.getChildren();
        return list;
    }

    public List getElementsByTagName(String NodeName) {
        Element Parent = null;
        Element ParentNode = GetElementByName(NodeName);
        return BeginEnumerate(ParentNode);
    }

    public Element Enumerate(List nodelist, int Index) {
        Element node = null;
        if (Index < nodelist.size()) {
            node = (Element) nodelist.get(Index);
        }
        return node;
    }

    public void EndEnumerate() {
    }

    //     public Element GetElementByName(String ElementName)
//     {
//          ElementFilter ef = new ElementFilter(ElementName);
//          return getElementByFilter(this.Root, ef);
//     }
//
//
//
//     public Element GetElementByName(Element e, String Name)
//     {
//          ElementFilter ef = new ElementFilter(Name);
//          return getElementByFilter(e, ef);
//     }
    public Element GetElementByName(String name) {

        return getElementByname(this.Root, name);
    }

    public Element getElementByname(Element element, String filter) {

        if (element.getChild(filter) == null) {
            List list = element.getChildren();
            java.util.Iterator it = list.iterator();
            while (it.hasNext()) {
                Object object = it.next();
                if (object instanceof Element) {
                    Object el = getElementByname((Element) object, filter);
                    if (el != null) {
                        return (Element) el;
                    }
                }
            }
        } else {
            return element.getChild(filter);
        }
        return null;

    }

    public Element AddChildElement(Element ParentNode, String ElementName) {
        Element E = null;
        E = CreateElement(ElementName);
        return AddChildElement(ParentNode, E);
    }

    public Element AddChildElement(Element ParentNode, Element E) {
        if (ParentNode == null) ParentNode = this.Root;
        ParentNode.addContent(E);
        return E;
    }

    public static void SetElementValue(Element node, String Name, String Value) {
        if (Value == null) Value = "";
        if (node != null) {
            Value = RepleaceValue1(Value);
            node.setAttribute(Name, Value);
        }
    }

    public static String GetElementValue(Element node, String Name) {
        String Value = "";
        try {
            Value = node.getAttribute(Name).getValue();
        } catch (Exception e) {
        }
        return Value;
    }

    public static String GetElementValue(Element node, String Name, String Default) {
        String Value = Default;
        try {
            Value = node.getAttribute(Name).getValue();
        } catch (Exception e) {
        }
        return Value;
    }

    public static String RepleaceValue1(String Value) {
        return Value;
    }

    public static String RepleaceValue2(String Value) {
        return Value;
    }

    public String GetRootXMLString() {
        return GetXMLString(this.Doc);
    }

    public String GetXMLString(Document doc) {
        try {
            XMLOutputter Out = new XMLOutputter();
            String Res = Out.outputString(doc);
            return Res;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Element CopyElementValue(Element srcnode, Element desnode) {
        if ((srcnode != null) && (desnode != null)) {
            List attrs = srcnode.getAttributes();
            int Count = attrs.size();
            for (int i = 0; i < Count; ++i) {
                Attribute attr = (Attribute) attrs.get(i);
                if (attr != null) {
                    String Name = attr.getName();
                    String Value = attr.getValue();
                    desnode.setAttribute(Name, Value);
                }
            }
        }
        return desnode;
    }

    public Element getPreviousSibling(Element e) {
        if (e == null) return null;
        Element ParentNode = e.getParentElement();
        if (ParentNode == null) return null;
        List list = ParentNode.getChildren();
        int Index = list.indexOf(e);
        if (Index - 1 >= 0) {
            return (Element) list.get(Index - 1);
        }
        return null;
    }

    public Element getNextSibling(Element e) {
        if (e == null) return null;
        Element ParentNode = e.getParentElement();
        if (ParentNode == null) return null;
        List list = ParentNode.getChildren();
        int Index = list.indexOf(e);
        if (Index + 1 < list.size()) {
            return (Element) list.get(Index + 1);
        }
        return null;
    }

    public boolean SaveToFile(String PathName) {
        boolean Res = false;
        String DataObject = GetRootXMLString();
        try {
            FileOutputStream FOS = new FileOutputStream(PathName);
            FOS.write(DataObject.getBytes());
            FOS.flush();
            FOS.close();
            Res = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res;
    }
}
